package com.degon.hsyl.imagepreview.activity;

import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.TextView;
import com.degon.hsyl.imagepreview.R;
import com.degon.hsyl.imagepreview.adapter.ImageViewPagerAdapter;

import java.io.File;
import java.util.ArrayList;

public class ImageGalleryActivity extends FragmentActivity {

    private ViewPager viewPager;
    private TextView indicatorText;
    private int firstIndex;
    private ArrayList<String> urls;
    private String current;
    private ImageViewPagerAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //设置状态栏颜色为黑色
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().setStatusBarColor(getResources().getColor(R.color.black));

        setContentView(R.layout.activity_image_gallery);

        //初始化组件
        initViews();

        //获取页面参数
        Intent intent = getIntent();
        current = intent.getStringExtra("current");

        if (current.startsWith("_doc")) {
            current = getPrefix() + current.substring(1);
        }

        urls = intent.getStringArrayListExtra("urls");
        handleUrls();

        firstIndex = getFirstIndex(urls, current);

        //设置indicator
        String content = (firstIndex + 1) + "/" + urls.size();
        indicatorText.setText(content);

        //加载图片
        adapter = new ImageViewPagerAdapter(urls, this, ImageGalleryActivity.this);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(firstIndex);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                String content = (position + 1) + "/" + urls.size();
                indicatorText.setText(content);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initViews() {
        viewPager = findViewById(R.id.view_pager);
        indicatorText = findViewById(R.id.indicator_text);
    }

    private int getFirstIndex(ArrayList<String> urls, String current) {

        if (isNumeric(current)) {
            return Integer.parseInt(current);
        } else {
            return getIndexOfUrl(urls, current);
        }

    }

    //获取指定url在url列表中的下标
    private int getIndexOfUrl(ArrayList<String> urls, String url) {
        int result = -1;
        for (int i = 0; i < urls.size(); i++) {
            if (urls.get(i).equals(url)) {
                result = i;
                break;
            }
        }
        return result;
    }

    /**
     * 判断字符串是否是数字
     *
     * @param str 需要判断的字符串
     * @return true:是 false:不是
     */
    private boolean isNumeric(String str) {
        boolean result = true;
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) {
                result = false;
                break;
            }
        }
        return result;
    }

    private void handleUrls() {

        for (int i = 0; i < urls.size(); i++) {
            if (urls.get(i).startsWith("_doc")) {
                String newUrl = getPrefix() + urls.get(i).substring(1);
                urls.set(i, newUrl);
            }
        }
    }

    private String getPrefix() {
        File packageFile = getExternalCacheDir().getParentFile();
        return packageFile.getPath() + "/apps/__UNI__F520A28/";
    }
}